﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SH.STD.Model;
using System.Data;

namespace SH.STD.Web.Bonus
{
    public partial class BonusProjectList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.SetTab(TabType.Bonus);

            if (!Page.IsPostBack)
            {
                this.BindData(this.GetSql());
                this.BindProject();
            }
        }

        private void BindData(string sql)
        {
            var dt = Cmn.DB.getDataTable(sql);

            this.rptProjects.DataSource = dt;
            this.rptProjects.DataBind();
            this.lblCnt1.Text = dt.Rows.Count.ToString();
        }

        private void BindProject()
        {
            var sql = "select ID,Name from Bonus_Project order by ID";
            this.ddlProject.Items.Clear();
            this.ddlProject.Items.Add(new ListItem("-请选择-", "-1"));
            var dt = Cmn.DB.getDataTable(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlProject.Items.Add(new ListItem(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString()));
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindData(this.GetSql());
        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.txtEmpName.Text = String.Empty;
            this.txtStartDate.Text = String.Empty;
            this.txtEndDate.Text = String.Empty;
            this.ddlProject.SelectedIndex = -1;

            this.BindData(this.GetSql());
        }

        private string GetSql()
        {
            var ret = @"
select a.No as ProjectNo,a.ID as ProjectID,a.Name as ProjectName,c.Name as EmpName,b.Amount as Amount,b.Month as Month,d.Name as DepName,a.Source as Source
from Bonus_Project a
left join Bonus b
on a.ID=b.ProjectID
left join Employee c
on b.EmployeeID=c.ID
join Department d
on a.DepID=d.ID
where ";
            var where = String.Empty;
            if (this.ddlProject.SelectedIndex > 0)
            {
                where += "a.Name like '%" + this.ddlProject.SelectedItem.Text + @"%' and ";
            }
            if (!String.IsNullOrEmpty(this.txtStartDate.Text.Trim()))
            {
                where += "b.Month>='" + this.txtStartDate.Text.Trim() + "' and ";
            }
            if (!String.IsNullOrEmpty(this.txtEndDate.Text.Trim()))
            {
                where += "b.Month<='" + this.txtEndDate.Text.Trim() + "' and ";
            }
            if (!String.IsNullOrEmpty(this.txtEmpName.Text.Trim()))
            {
                where += @"c.Name like '%" + this.txtEmpName.Text.Trim() + @"%' and ";
            }
            ret = where == String.Empty ? ret.Substring(0, ret.LastIndexOf("where ")) : ret + " " + where.Substring(0, where.LastIndexOf("and "));

            return ret;
        }

        public static string ExportTable(DataTable dt)
        {
            var columnNames = new Dictionary<string, string>();
            columnNames.Add("项目编号", "ProjectNo");
            columnNames.Add("项目名称", "ProjectName");
            columnNames.Add("发放人员", "EmpName");
            columnNames.Add("发放时间", "Month");
            columnNames.Add("发放金额", "Amount");
            columnNames.Add("列支渠道", "Source");
            columnNames.Add("部门审查", "DepName");
            string ret = "<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">";
            ret += "<tr style=\"font-weight: bold; white-space: nowrap;\">";
            foreach (var col in columnNames)
            {
                ret += "<td>" + col.Key + "</td>";
            }
            ret += "</tr>";
            foreach (DataRow row in dt.Rows)
            {
                ret += "<tr>";
                foreach (var col in columnNames)
                {
                    var colValue = (row[col.Value].ToString() != "undefined" ? row[col.Value].ToString() : "");
                    if (col.Value.Contains("Month") && !String.IsNullOrEmpty(colValue))
                    {
                        colValue = DateTime.Parse(colValue).ToString("yyyy-MM-dd");
                    }
                    ret += "<td>" + colValue + "</td>";
                }
                ret += "</tr>";
            }
            ret += "</table>";

            return ret;
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            var dt = Cmn.DB.getDataTable(this.GetSql());
            ExportDsToXls(this.Page, HttpUtility.UrlEncode(this.lblTitle.Text, System.Text.Encoding.UTF8), dt);
        }

        public static void ExportDsToXls(Page page, string fileName, DataTable dt)
        {
            page.Response.Clear();
            page.Response.Buffer = true;
            page.Response.Charset = "GB2312";
            page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
            page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            page.Response.ContentType = "application/ms-excel";
            page.EnableViewState = false;
            page.Response.Write(ExportTable(dt));
            page.Response.End();
        }
    }
}